import 'package:flutter/material.dart';
import 'dart:ui';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import '../home_tab/story_tab.dart';
import '../home_tab/china_tab.dart';
import '../home_tab/child_song_tab.dart';
import '../home_tab/mine_tab.dart';
import 'package:bear_story/views.dart';
import '../main.dart';
import '../bean/beans.dart';
import '../pref.dart' as pref;

class HomePage extends StatelessWidget with OnJinPingBooksUpdateListener {
  final List<Tab> tabs = <Tab>[new Tab(text: '故事'), new Tab(text: '国学'), new Tab(text: '儿歌'), new Tab(text: '我的')];

  HomePage() {
    addOnJinPingBooksUpdateListener(this);
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    var devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
    var textScaleFactor = MediaQuery.of(context).textScaleFactor;
    var size = MediaQuery.of(context).size;
    print("HomePage--size:${size.width}x${size.height}");
    print("HomePage--devicePixelRatio:$devicePixelRatio");
    print("HomePage--textScaleFactor:$textScaleFactor");
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle.light.copyWith(statusBarBrightness: Brightness.dark));
    final List<Widget> tabViews = [StoryTabView(), ChinaTabView(), ChildSongTabView(), MineTabView()];
    return DefaultTabController(
        length: tabs.length,
        child: new Scaffold(
            body: SafeArea(
                child: ConstrainedBox(
          constraints: BoxConstraints.expand(),
          child: Stack(
            children: <Widget>[
              Column(
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  TabBar(
                    tabs: tabs,
//                    isScrollable: true,
                    labelColor: Colors.black,
//                    labelPadding: EdgeInsets.only(top: 0, bottom: 0, left: 12, right: 12),
                    labelStyle: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
                    unselectedLabelStyle: TextStyle(fontSize: 18),
                    unselectedLabelColor: Color(0xFF666666),
                    indicatorColor: Color(0xFFDD4D4D),
                    indicatorWeight: 3,
                    indicatorSize: TabBarIndicatorSize.label,
                    indicatorPadding: EdgeInsets.only(top: 0, bottom: 0, left: 0, right: 0),
//                    indicator: BoxDecoration(
//                        color: Colors.yellow,
//                        borderRadius:
//                            new BorderRadius.all(Radius.circular(30))),
                  ),
                  Flexible(
                    child: TabBarView(children: tabViews),
                    flex: 1,
                  ),
                ],
              ),
              Align(
                alignment: FractionalOffset.bottomCenter,
                child: PlayerControllerBarView(),
              )
            ],
          ),
        ))));
  }

  @override
  void onJinPingBooksUpdate(Book book) {
    // TODO: implement onJinPingBooksUpdate
    checkNowPlayList(book);
  }

  Future checkNowPlayList(Book book) async {
    var v = await channelCheckNowPlayList();
    if (!v) {
      if (getNowPlayBook() == null) {
        print("HomePage--getNowPlayBook==null");
        print("HomePage--book==${book.toJson()}");
        var chapters = await getChapters(book, 1, 20);
        playList(chapters, 0, book, false);
      } else {
        if (getNowPlayChapters() == null || getNowPlayChapters().length == 0) {
          int index = await pref.getNowPlayIndex();
          var chapters = await getChapters(getNowPlayBook(), 1, 20);
          playList(chapters, index, getNowPlayBook(), false);
        }
      }
    }
    return true;
  }
}
